<template>
<div style="width: 400px;text-align:center;margin: 5px auto">
  <h1 STYLE="margin-bottom: 10px;">知识库新增</h1>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="类型"  prop="kbType" >
      <el-input type="text" v-model="ruleForm.kbType" autocomplete="off"  ></el-input>
    </el-form-item>
  <el-form-item label="关键字"  prop="kbKeyword" >
      <el-input type="text" v-model="ruleForm.kbKeyword" autocomplete="off"  ></el-input>
    </el-form-item>
  <el-form-item label="语法"  prop="kbGrammar" >
      <el-input type="text" v-model="ruleForm.kbGrammar" autocomplete="off"  ></el-input>
    </el-form-item>
  <el-form-item label="描述" >
      <el-input type="text" v-model="ruleForm.desc" autocomplete="off" @keyup.native="autoSubmit($event)" ></el-input>
    </el-form-item>
    <el-form-item >
      <div style="text-align:center">
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="returnList">返回</el-button>
      </div>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "add",
  data(){
    const validateKbType = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入类型"));
      } else {
        callback();
      }
    };
    const validateKbKeyword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入关键字"));
      } else {
        callback();
      }
    };
    const validateKbGrammar = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入语法"));
      } else {
        callback();
      }
    };

    return{
      ruleForm: {
        kbType: '',
        kbKeyword: '',
        kbGrammar: '',
        desc: '',
      },

      rules: {
        kbType: [{ required: true,validator: validateKbType, trigger: "blur" }],
        kbKeyword: [{ required: true,validator: validateKbKeyword, trigger: "blur" }],
        kbGrammar: [{ required: true,validator: validateKbGrammar, trigger: "blur" }],
      }
    }

  },
  methods: {
    autoSubmit: function (ev) {
      if (ev.code === "Enter") {
        this.submitForm('ruleForm');
      }
    },
    submitForm(formName) {
      let _this = this;
      const param = {
        kbType: _this.ruleForm.kbType,
        kbKeyword: _this.ruleForm.kbKeyword,
        kbGrammar: _this.ruleForm.kbGrammar,
        desc: _this.ruleForm.desc,
      };
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            method: "POST",
            url: "/api/KnowledgeBase/insert",
            data: param
          })
            .then(res => {
              if (res.data.status == "1") {
                this.$message(res.data.message);
                _this.$router.push({path: "/knowledgebase/list"});
              } else {
                this.$alert(res.data.message, {
                  confirmButtonText: '确定',
                  callback: action => {
                  }
                });
                return false;
              }
            })
            .catch(err => {
              debugger;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    returnList(){
      this.$router.push({path: "/knowledgebase/list"});
    }
  }
}
</script>

<style scoped>

</style>
